{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.random.uniform(-3, 3, size=100)\n",
    "X = x. reshape(-1, 1)\n",
    "y = 0.5 * x**2 + x + 2 + np.random.normal(0, 1, size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import PolynomialFeatures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "poly = PolynomialFeatures(degree=2)\n",
    "poly.fit(X)\n",
    "X2 = poly.transform(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100, 3)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        , -1.34284888,  1.80324311],\n",
       "       [ 1.        , -0.18985858,  0.03604628],\n",
       "       [ 1.        , -1.58563134,  2.51422675],\n",
       "       [ 1.        ,  1.2149354 ,  1.47606802],\n",
       "       [ 1.        , -2.05874706,  4.23843944]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X2[:5,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.34284888],\n",
       "       [-0.18985858],\n",
       "       [-1.58563134],\n",
       "       [ 1.2149354 ],\n",
       "       [-2.05874706]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:5,:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第一列是1，第二列是原数据，第三列是原数据的平方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "\n",
    "lin_reg2 = LinearRegression()\n",
    "lin_reg2.fit(X2, y)\n",
    "y_predict2 = lin_reg2.predict(X2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAgAElEQVR4nO3deXxU9bnH8c9DiBJQjAqoBBVEgatFQePS4goorpgibtWq7a37vnBBrVXaUlBcW3u1VtxwgwIGEQWr4JVaRYKgiGzuEgGjEi0QJCS/+8dJYpaZZJYzM+dkvu/XKy+Sycyc3wyTZ37znOf3/Mw5h4iIhE+bTA9AREQSowAuIhJSCuAiIiGlAC4iElIK4CIiIdU2nQfr1KmT6969ezoPKSISegsXLvzaOde58eVpDeDdu3enpKQknYcUEQk9M/ss0uVKoYiIhJQCuIhISCmAi4iElAK4iEhIKYCLiIRUWqtQRETCqnhRKeNnr+DL8gq65ucxYkhvivoXZHRMCuAiIi0oXlTKjdOWUFFZBUBpeQU3TlsCkNEgrgAuItKC8bNX1AXvWhWVVYyfvSJqAE/HjF05cBGRFnxZXhHX5bUz9tLyChzejP3aSYv5bfESX8elAC4i0oKu+XlxXR5pxu6Ap976nOJFpb6NSwFcRKQFI4b0Ji83p8Flebk5jBjSO+L1o83MHV5w94sCuIhIC4r6FzB2WF8K8vMwoCA/j7HD+kbNaUebmUP04J4IncQUE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Pw5pjZPvV+HAosz9RY/GBmxwMjgaHOuU2ZHk+WWwDsY2Y9zGwb4Czg+QyPKevVnPibACxzzt2d6fEkw8w611aamVkeMBifYlhYqlCmAr3xqh0+Ay5xzpVmdlSJM7MPgW2Bb2oueiusVTVm9nPgL0BnoBxY7JwbktlRxcfMTgTuBXKAR5xzYzI8pISY2TPA0XhtS9cBtzrnJmR0UAkys8OBecASvL97gJuccy9mblSJMbP9gcfxXl9tgMnOud/7ct9hCOAiItJUKFIoIiLSlAK4iEhIKYCLiISUAriISEgpgIuIhJQCuIhISCmAi4iE1P8DKZ195Yt1SBYAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x, y)\n",
    "plt.plot(np.sort(x), y_predict2[np.argsort(x)], color='r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.        , 1.01723515, 0.46407147])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lin_reg2.coef_   #0是对X2是第一列数据拟合的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.1789150996943945"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lin_reg2.intercept_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关于PolynomialFeatures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 2)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.arange(1,11).reshape(-1, 2)\n",
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2],\n",
       "       [ 3,  4],\n",
       "       [ 5,  6],\n",
       "       [ 7,  8],\n",
       "       [ 9, 10]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 6)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "poly = PolynomialFeatures(degree=2)\n",
    "poly.fit(X)\n",
    "X2 = poly.transform(X)\n",
    "X2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1.,   1.,   2.,   1.,   2.,   4.],\n",
       "       [  1.,   3.,   4.,   9.,  12.,  16.],\n",
       "       [  1.,   5.,   6.,  25.,  30.,  36.],\n",
       "       [  1.,   7.,   8.,  49.,  56.,  64.],\n",
       "       [  1.,   9.,  10.,  81.,  90., 100.]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第一列：1，即0次幂\n",
    "第二列：x1，1次幂\n",
    "第三列：x2，1次幂\n",
    "第四列：x1^2，2次幂\n",
    "第五列：x1*x2，2次幂\n",
    "第六列：x2^2，2次幂"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
